/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import concursos.concurso.Candidato;
import controllers.exceptions.NonexistentEntityException;
import controllers.exceptions.PreexistingEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import concursos.concurso.Inscricao;

/**
 *
 * @author lucena
 */
public class CandidatoJpaController {

    public CandidatoJpaController() {
        emf = Persistence.createEntityManagerFactory("concursosPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Candidato candidato) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Inscricao inscricao = candidato.getInscricao();
            if (inscricao != null) {
                inscricao = em.getReference(inscricao.getClass(), inscricao.getId());
                candidato.setInscricao(inscricao);
            }
            em.persist(candidato);
            if (inscricao != null) {
                Candidato oldCandidatoOfInscricao = inscricao.getCandidato();
                if (oldCandidatoOfInscricao != null) {
                    oldCandidatoOfInscricao.setInscricao(null);
                    oldCandidatoOfInscricao = em.merge(oldCandidatoOfInscricao);
                }
                inscricao.setCandidato(candidato);
                inscricao = em.merge(inscricao);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCandidato(candidato.getId()) != null) {
                throw new PreexistingEntityException("Candidato " + candidato + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Candidato candidato) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Candidato persistentCandidato = em.find(Candidato.class, candidato.getId());
            Inscricao inscricaoOld = persistentCandidato.getInscricao();
            Inscricao inscricaoNew = candidato.getInscricao();
            if (inscricaoNew != null) {
                inscricaoNew = em.getReference(inscricaoNew.getClass(), inscricaoNew.getId());
                candidato.setInscricao(inscricaoNew);
            }
            candidato = em.merge(candidato);
            if (inscricaoOld != null && !inscricaoOld.equals(inscricaoNew)) {
                inscricaoOld.setCandidato(null);
                inscricaoOld = em.merge(inscricaoOld);
            }
            if (inscricaoNew != null && !inscricaoNew.equals(inscricaoOld)) {
                Candidato oldCandidatoOfInscricao = inscricaoNew.getCandidato();
                if (oldCandidatoOfInscricao != null) {
                    oldCandidatoOfInscricao.setInscricao(null);
                    oldCandidatoOfInscricao = em.merge(oldCandidatoOfInscricao);
                }
                inscricaoNew.setCandidato(candidato);
                inscricaoNew = em.merge(inscricaoNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                long id = candidato.getId();
                if (findCandidato(id) == null) {
                    throw new NonexistentEntityException("The candidato with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Candidato candidato;
            try {
                candidato = em.getReference(Candidato.class, id);
                candidato.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The candidato with id " + id + " no longer exists.", enfe);
            }
            Inscricao inscricao = candidato.getInscricao();
            if (inscricao != null) {
                inscricao.setCandidato(null);
                inscricao = em.merge(inscricao);
            }
            em.remove(candidato);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Candidato> findCandidatoEntities() {
        return findCandidatoEntities(true, -1, -1);
    }

    public List<Candidato> findCandidatoEntities(int maxResults, int firstResult) {
        return findCandidatoEntities(false, maxResults, firstResult);
    }

    private List<Candidato> findCandidatoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Candidato as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Candidato findCandidato(long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Candidato.class, id);
        } finally {
            em.close();
        }
    }

    public int getCandidatoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Candidato as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
